package com.gem.service.impl;

import com.gem.domain.CDailySpending;
import com.gem.domain.User;
import com.gem.mapper.CDailySpendingMapper;
import com.gem.service.CDailySpendingService;
import com.gem.util.ConditionUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.NoTransactionException;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author Apolar_GuanYH-管阳红
 * Description: 日常经费服务实现
 * create 2019-10-19 19:58
 * Modified By:
 */
@Service
public class CDailySpendingServiceImpl implements CDailySpendingService {

    private CDailySpendingMapper cdsMapper;

    @Autowired
    public void setDsMapper(CDailySpendingMapper dsMapper) {
        this.cdsMapper = dsMapper;
    }

    //查询所有
    @Override
    public List<CDailySpending> selectAllDS() {
        return cdsMapper.selectAllDS();
    }

    //查询所有市场
    @Override
    public List<CDailySpending> selectDSForMarketService() {
        return cdsMapper.selectDSForMarket();
    }

    //按员工id查询
    @Override
    public List<CDailySpending> selectDSByUidService(Integer uId) {
        return cdsMapper.selectDSByUid(uId);
    }

    //添加
    @Override
    public boolean addDSService(CDailySpending cDailySpending) {

        if (cDailySpending.getDsEntertainLeader() == null) {
            cDailySpending.setDsEntertainLeader(0.00);
        }
        if (cDailySpending.getDsEntertainStu() == null) {
            cDailySpending.setDsEntertainStu(0.00);
        }
        if (cDailySpending.getDsOther() == null) {
            cDailySpending.setDsOther(0.00);
        }
        if (cDailySpending.getDsOtherState() == null) {
            cDailySpending.setDsOtherState("");
        }
        if (cDailySpending.getDsRemark() == null) {
            cDailySpending.setDsRemark("");
        }
        if (cDailySpending.getDsServices() == null) {
            cDailySpending.setDsServices(0.00);
        }
        if (cDailySpending.getDsStayCount() == null) {
            cDailySpending.setDsStayCount(0);
        }
        if (cDailySpending.getDsStayPeople() == null) {
            cDailySpending.setDsStayPeople("");
        }
        if (cDailySpending.getDsStayTotalprice() == null) {
            cDailySpending.setDsStayTotalprice(0.00);
        }
        if (cDailySpending.getDsStayUnitprice() == null) {
            cDailySpending.setDsStayUnitprice(0.00);
        }
        if (cDailySpending.getDsTotal() == null) {
            cDailySpending.setDsTotal(0.00);
        }
        if (cDailySpending.getDsTrafficOther() == null) {
            cDailySpending.setDsTrafficOther(0.00);
        }
        if (cDailySpending.getDsTrafficTotal() == null) {
            cDailySpending.setDsTrafficTotal(0.00);
        }
        if (cDailySpending.getDsTrafficTaxi() == null) {
            cDailySpending.setDsTrafficTaxi(0.00);
        }
        return cdsMapper.addDS(cDailySpending);
    }

    //修改
    @Override
    public boolean modifyDSService(CDailySpending cDailySpending) {

        if (cDailySpending.getDsEntertainLeader() == null) {
            cDailySpending.setDsEntertainLeader(0.00);
        }
        if (cDailySpending.getDsEntertainStu() == null) {
            cDailySpending.setDsEntertainStu(0.00);
        }
        if (cDailySpending.getDsOther() == null) {
            cDailySpending.setDsOther(0.00);
        }
        if (cDailySpending.getDsOtherState() == null) {
            cDailySpending.setDsOtherState("");
        }
        if (cDailySpending.getDsRemark() == null) {
            cDailySpending.setDsRemark("");
        }
        if (cDailySpending.getDsServices() == null) {
            cDailySpending.setDsServices(0.00);
        }
        if (cDailySpending.getDsStayCount() == null) {
            cDailySpending.setDsStayCount(0);
        }
        if (cDailySpending.getDsStayPeople() == null) {
            cDailySpending.setDsStayPeople("");
        }
        if (cDailySpending.getDsStayTotalprice() == null) {
            cDailySpending.setDsStayTotalprice(0.00);
        }
        if (cDailySpending.getDsStayUnitprice() == null) {
            cDailySpending.setDsStayUnitprice(0.00);
        }
        if (cDailySpending.getDsTotal() == null) {
            cDailySpending.setDsTotal(0.00);
        }
        if (cDailySpending.getDsTrafficOther() == null) {
            cDailySpending.setDsTrafficOther(0.00);
        }
        if (cDailySpending.getDsTrafficTotal() == null) {
            cDailySpending.setDsTrafficTotal(0.00);
        }
        if (cDailySpending.getDsTrafficTaxi() == null) {
            cDailySpending.setDsTrafficTaxi(0.00);
        }
        return cdsMapper.modifyDS(cDailySpending);
    }

    //按id查询
    @Override
    public CDailySpending selectDSByIdService(Integer id) {
        return cdsMapper.selectDSById(id);
    }

    //单个删除
    @Override
    public boolean deleteDSByIdService(Integer id) {
        return cdsMapper.deleteDSById(id);
    }

    //批量删除
    @Override
    @Transactional
    public boolean deleteDSByIdsService(String[] dsIdArr) {
        try {
            boolean flag = true;
            if (dsIdArr.length != 0) {
                for (String dsId : dsIdArr) {
                    if (!dsId.trim().equals("")) {
                        if (!deleteDSByIdService(Integer.parseInt(dsId))) {
                            flag = false;
                            break;
                        }
                    }
                }
                if (!flag) {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                }
            }
            return flag;
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }

    //分页显示
    @Override
    public PageInfo<CDailySpending> selectDSByPage(int currentPage, int pageSize, HttpSession session) {
        PageHelper.startPage(currentPage, pageSize);
        PageInfo<CDailySpending> pageInfo;
        User user = (User) session.getAttribute("user");
        if (user.getAuthority() == 1 && "市场".equals(user.getDepartment())) {
            pageInfo = new PageInfo<>(cdsMapper.selectDSForMarket());
        } else if (user.getAuthority() == 1 && "财务".equals(user.getDepartment())) {
            pageInfo = new PageInfo<>(cdsMapper.selectAllDS());
        } else {
            pageInfo = new PageInfo<>(cdsMapper.selectDSByUid(user.getUid()));
        }
        return pageInfo;
    }

    //手机端分页显示
    @Override
    public PageInfo<CDailySpending> selectDSByPageM(int currentPage, int pageSize, HttpSession session) {
        PageHelper.startPage(currentPage, pageSize);
        User user = (User) session.getAttribute("user");
        return new PageInfo<>(cdsMapper.selectDSByUid(user.getUid()));
    }

    //查询分页显示
    @Override
    public PageInfo<CDailySpending> selectDSByConditionByPage(int currentPage, int pageSize, ConditionUtil cu, HttpSession session) {
        PageHelper.startPage(currentPage, pageSize);
        User user = (User) session.getAttribute("user");
        if (user.getAuthority() == 1 && "市场".equals(user.getDepartment())) {
            cu.setDepartment("市场");
        } else if (!(user.getAuthority() == 1 && "财务".equals(user.getDepartment()))) {
            cu.setUId(user.getUid());
        }
        return new PageInfo<>(cdsMapper.selectDSByCondition(cu));
    }

    //手机端查询分页显示
    @Override
    public PageInfo<CDailySpending> selectDSByConditionByPageM(int currentPage, int pageSize, ConditionUtil cu, HttpSession session) {
        PageHelper.startPage(currentPage, pageSize);
        User user = (User) session.getAttribute("user");
        cu.setUId(user.getUid());
        return new PageInfo<>(cdsMapper.selectDSByCondition(cu));
    }

    //上传Excel数据（开启事务）
    @Override
    public boolean uploadExcelService(List<CDailySpending> list) {
        try {
            boolean flag = true;
            for (CDailySpending cDailySpending : list) {
                if (!cdsMapper.addDS(cDailySpending)) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                return true;
            } else {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            }
        } catch (NoTransactionException e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return false;
    }


}
